package me.zhengjie.modules.vip.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.vip.domain.RechargeRecord;
import me.zhengjie.modules.vip.service.RechargeRecordService;
import me.zhengjie.modules.vip.domain.vo.RechargeRecordQueryCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

@RestController
@RequiredArgsConstructor
@Api(tags = "充值记录管理")
@RequestMapping("/api/rechargeRecords")
public class RechargeRecordController {

    private final RechargeRecordService rechargeRecordService;

    @GetMapping
    @Log("查询充值记录")
    @ApiOperation("查询充值记录")
    @PreAuthorize("@el.check('rechargeRecord:list')")
    public ResponseEntity<PageResult<RechargeRecord>> query(RechargeRecordQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(rechargeRecordService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/member/{memberId}")
    @Log("查询会员充值记录")
    @ApiOperation("查询会员充值记录")
    @PreAuthorize("@el.check('rechargeRecord:list')")
    public ResponseEntity<PageResult<RechargeRecord>> queryByMember(@PathVariable Long memberId, Page<Object> page){
        RechargeRecordQueryCriteria criteria = new RechargeRecordQueryCriteria();
        criteria.setMemberId(memberId);
        return new ResponseEntity<>(rechargeRecordService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/recharge/{cardId}")
    @Log("查询会员卡充值记录")
    @ApiOperation("查询会员卡充值记录")
    @PreAuthorize("@el.check('rechargeRecord:list')")
    public ResponseEntity<PageResult<RechargeRecord>> queryByCard(@PathVariable Long cardId, Page<Object> page){
        RechargeRecordQueryCriteria criteria = new RechargeRecordQueryCriteria();
        criteria.setCardId(cardId);
        return new ResponseEntity<>(rechargeRecordService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping(value = "/download")
    @Log("导出充值记录")
    @ApiOperation("导出充值记录")
    @PreAuthorize("@el.check('rechargeRecord:list')")
    public void download(HttpServletResponse response, RechargeRecordQueryCriteria criteria) throws IOException {
        rechargeRecordService.download(rechargeRecordService.queryAll(criteria), response);
    }

    @PostMapping("revoke/{recordId}")
    @Log("撤销充值记录")
    @ApiOperation("撤销充值记录")
    @PreAuthorize("@el.check('rechargeRecord:edit')")
    public ResponseEntity<Object> revoke(@PathVariable Long recordId){
        rechargeRecordService.revoke(recordId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}